package com.taobao.ad.hadoop;

import java.util.*;
import com.taobao.ad.hadoop.common.Pair;
import com.taobao.ad.hadoop.common.TopK;
 
/**
 * Hello world!
 *
 */
public class App 
{
	static List<Integer> getRandomArray(int n){
		Random rnd = new Random();
		List<Integer> list = new ArrayList<Integer>();
		for(int i = 0; i < n; i ++){
			list.add(i + 1);
		}
		for(int i = 0; i < n; i++){
			int p1 = Math.abs(rnd.nextInt() % n);
			int p2 = Math.abs(rnd.nextInt() % n);
			int x = list.get(p1);
			list.set(p1, list.get(p2));
			list.set(p2, x);
		}
		return list;
	}
	public static void printList(List<Pair> pairList){
		for(int i=0; i < pairList.size(); i++){
    		System.out.print(pairList.get(i).getKey() + ",");
    	}
		System.out.println();
	}
	
	public static void test(){
		int n = 20000;
    	int k = 20000;
    	List<Integer> rndList = getRandomArray(n);
    	List<Pair> pairList = new ArrayList<Pair>();
    	for(Integer x: rndList){
    		pairList.add(new Pair(x,Double.valueOf(x)));
    	}
    	//---------------------------------------------------------------
    	long d1 = Calendar.getInstance().getTimeInMillis();
    	TopK.getTopK1(pairList, k);
    	
    	long d2 = Calendar.getInstance().getTimeInMillis(); 
    	System.out.println(d1);
    	System.out.println(d2);
    	long span = d2 - d1;
    	System.out.println("TimeUse:" + span + "ms"); 
    	System.out.println("\n--------------------------------------------");
    	 
    	d1 = Calendar.getInstance().getTimeInMillis();
    	TopK.getTopK2(pairList, k);
    	
    	d2 = Calendar.getInstance().getTimeInMillis(); 
    	System.out.println(d1);
    	System.out.println(d2);
    	span = d2 - d1;
    	System.out.println("TimeUse:" + span + "ms"); 
    	System.out.println("\n--------------------------------------------");
    
    	d1 = Calendar.getInstance().getTimeInMillis();
    	TopK.getTopK3(pairList, k);
    	
    	d2 = Calendar.getInstance().getTimeInMillis(); 
    	System.out.println(d1);
    	System.out.println(d2);
    	span = d2 - d1;
    	System.out.println("TimeUse:" + span + "ms"); 
    	System.out.println("\n--------------------------------------------");
    	
    	d1 = Calendar.getInstance().getTimeInMillis();
    	TopK.getTopK4(pairList, k);
    	
    	d2 = Calendar.getInstance().getTimeInMillis(); 
    	System.out.println(d1);
    	System.out.println(d2);
    	span = d2 - d1;
    	System.out.println("TimeUse:" + span + "ms"); 
    	System.out.println("\n--------------------------------------------");
	}
	
 
    public static void main( String[] args ){ 
        test();
    	 
    }
}












